import datetime

# from lianjie import func1
import pymysql


def func1(yuju, neirong, str1, str2):
    con = pymysql.connect(host='101.200.51.214', port=3306, user='root', password='123456',
                          database='messages', charset='utf8', autocommit=True)
    try:
        with con.cursor(cursor=pymysql.cursors.DictCursor) as cursor:
            result = cursor.execute(yuju, neirong)
            if result:
                print(str1)
            else:
                print(str2)
    except:
        print('输入内容错误请重新输入')
    finally:
        con.close()


def func2(yuju,neirong, str1, str2):
    con = pymysql.connect(host='101.200.51.214', port=3306, user='root', password='123456',
                          database='messages', charset='utf8', autocommit=True)
    try:
        with con.cursor(cursor=pymysql.cursors.DictCursor) as cursor:
            result = cursor.execute(yuju,neirong)
            if result:
                print(str1)
                list1=cursor.fetchall()
                for i in list1:
                    print('编号:'+str(i['ms_id'])+'\t发件人：'+i['sender']+'\t标题：'+i['title'])
                    print('信息：'+i['content'])
                    print('=====================================================================')

            else:
                print('=====================================================================')
                print(str2)
                print('=====================================================================')
    except:
        print('输入内容错误请重新输入')
    finally:
        con.close()


def funclogin(name):
    while True:
        print('=================================================')
        print('1.发送消息\t 2.查看未读消息\t3.退出登录')
        print('=================================================')
        choose1 = input('请选择：')

        if choose1 == '1':
            gettername = input('你要发送给哪位呢：')
            m_title = input('邮件标题：')
            m_content = input('邮件内容：')
            now_time = datetime.datetime.now()
            yuju = 'insert into tb_messages values (default,%s,%s,%s,%s,%s,0,0,%s)'
            neirong= (name, gettername, m_title, m_content, now_time, name )
            str1 = '发送成功'
            str2 = '发送失败'
            func1(yuju, neirong, str1, str2)

        elif choose1 == '2':
            while True:
                print('===================================================')
                print('1.读取未读\t2.标记信息\t3.删除已标记信息\t4.退出当前')
                print('===================================================')
                choose2=input('小婉提示您做出选择呢(*^__^*) : ')

                if choose2 == '1':
                    # 查看未读消息
                    yuju = 'select ms_id,sender,title,content from tb_messages where getter=%s and read_m=0 '
                    nirong = (name,)
                    str1 = '小婉帮您获取到了信息哟'
                    str2 = '小婉没有找到任何信息哦'
                    func2(yuju, nirong, str1, str2)

                elif choose2 == '2':
                    while True:
                        print('===================================================================')
                        print('1.标记全部信息\t 2.取消全部标记\t 3.根据编号标记\t4.根据编号取消\t 5.退出')
                        print('===================================================================')
                        # 标记信息
                        choose3=input('选择标记形式：')

                        if choose3=='1':
                            # 标记全部
                            yuju = 'update tb_messages set read_m=1 where getter=%s '
                            nirong = (name,)
                            str1 = '标记成功！！！交给小碗吧~~~=v='
                            str2 = '小婉没有找到任何信息哦'
                            func1(yuju, nirong, str1, str2)

                        elif choose3=='2':
                            # 取消全部
                            yuju = 'update tb_messages set read_m=0 where getter=%s '
                            nirong = (name,)
                            str1 = '小婉很努力了'
                            str2 = '小婉没有找到任何信息哦'
                            func1(yuju, nirong, str1, str2)

                        elif choose3 == '3':
                            number = int(input('请输入标记编号：'))
                            # 更具编号标记
                            yuju = 'update tb_messages set read_m=1 where getter=%s and ms_id=%s '
                            nirong = (name,number)
                            str1 = '小婉很努力了'
                            str2 = '小婉没有找到任何信息哦'
                            func1(yuju, nirong, str1, str2)

                        elif choose3 == '4':
                            number1 = int(input('请输入取消编号：'))
                            # 根据标号取消
                            yuju = 'update tb_messages set read_m=0 where getter=%s and ms_id=%s '
                            nirong = (name,number1)
                            str1 = '小婉很努力了'
                            str2 = '小婉没有找到任何信息哦'
                            func1(yuju, nirong, str1, str2)

                        elif choose3 == '5':
                            break

                elif choose2 == '3':
                    # 删除标记内容
                    yuju = 'delete from tb_messages where getter=%s and read_m=1 '
                    nirong = (name,)
                    str1 = '删除已读成功'
                    str2 = '小婉没有找到任何信息哦'
                    func1(yuju, nirong, str1, str2)

                elif choose2 == '4':
                    break


        elif choose1 == '3':
            break